#pragma newdecls required
#pragma semicolon 1

#include <sdktools>

#define HUD_INTERVALL 						1.0
#define MAX_TARGET_DISTENCE 				2048.0

public Plugin myinfo =
{
	name        = "Team Healing",
	author      = "F1F88",
	description = "在屏幕中间显示瞄准的实体名字和HP值",
	version     = "1.0.0",
	url         = "https://gitee.com/f1f88/nmrih-chinatown-exploration"
};

public void OnPluginStart() {
	RegConsoleCmd("sm_sethp", Command_hp);
}

public void OnMapStart() {
	CreateTimer(HUD_INTERVALL, Timer_DisplayHud, 0, TIMER_REPEAT | TIMER_FLAG_NO_MAPCHANGE);
}

Action Command_hp(int client, int param2) {
	int ent = get_ent(client, MAX_TARGET_DISTENCE, client);
	SetEntProp(ent, Prop_Data, "m_iHealth", 100000);
	return Plugin_Continue;
}

Action Timer_DisplayHud(Handle timer, any data)
{
    for( int client=1; client<=MaxClients; ++client )
    {
        if( ! IsClientInGame(client) )
            continue;

        static int entity, health;
        static char classname[MAX_NAME_LENGTH];

        classname = NULL_STRING;
        entity = get_ent(client, MAX_TARGET_DISTENCE, client);

        if( entity > 0 && IsValidEntity(entity) )
        {
            if( (health = GetEntProp(entity, Prop_Data, "m_iHealth", 1)) <= 0 )
                continue;
            GetEntityClassname(entity, classname, MAX_NAME_LENGTH);
            PrintCenterText(client, "%s: %d HP", classname, health);
        }
    }
    return Plugin_Continue;
}


stock int get_ent(int client, float max_distence, any data=-1, float hitPos[3]={0.0, ...})
{
	float fEyePos[3], fEyeAngle[3];
	float fEndPos[3], fwd[3];
	int target_index;
	Handle trace;

	GetClientEyePosition(client, fEyePos);
	GetClientEyeAngles(client, fEyeAngle);

	GetAngleVectors(fEyeAngle, fwd, NULL_VECTOR, NULL_VECTOR);
	fEndPos[0] = fEyePos[0] + fwd[0] * max_distence;
	fEndPos[1] = fEyePos[1] + fwd[1] * max_distence;
	fEndPos[2] = fEyePos[2] + fwd[2] * max_distence;

	trace = TR_TraceRayFilterEx(fEyePos, fEndPos, MASK_VISIBLE, RayType_EndPoint, filter, data);
	if( TR_DidHit(trace) )  	TR_GetEndPosition(hitPos, trace);
	target_index = TR_GetEntityIndex(trace);
	delete trace;
	return target_index;
}

stock bool filter(int entity, int contentsMask, int client) {
	if( entity <= 0 || entity == client ) 	return false;
	return IsValidEntity(entity) || (0 < entity <= MaxClients && IsClientInGame(entity));
}
